package com.xyy.saas.payment.merchant.core.vo;

import com.xyy.saas.payment.common.enums.PictureEnum;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.CertTypeEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.vo.*;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;

/**
 * @Author: wuqiongda
 * @Date: 2019/10/24 9:29
 */
@Getter
@Setter
public class UserDto {
    /**
     * 支付平台用户ID
     */
    private String userId;
    /**
     * 可选值："business" 对公，"customer" 对私。对公用户请填写 "business"
     */
    private String type;
    /**
     * 客户类型
     */
    private String customerType;
    /**
     * 用户在ping++的编号
     */
    private String accNo;
    /**
     * 00:新增，01:修改
     */
    private String operateType;
    /**
     * key: 101：个人身份证正面，102：个人身份证反面，201：企业证件照片，202：法人身份证正面，203：法人身份证反面。对公用户需使用 201、202、203 上传三次
     * value: 图片 BASE64 编码，编码后的大小不能超过 1M，具体视场景而定（注意：请删除开头的：data:image/{{PIC_FMT}};base64, 字符串之后再提交）
     */
    private Map<String, String> picMap;
    /**
     * 可选值：png、jpg
     */
    /** 图片格式", name = "picFmt */
    private String picFmt;
    /**
     * 企业联系人", name = "ContactInfos
     */
    private List<ContactInfo> contactInfos;
    /**
     * 子商户应用名称", name = "displayName
     */
    private String displayName;
    /**
     * 子商户简称", name = "description
     */
    private String description;
    /**
     * 公司名称", name = "name
     */
    private String name;
    /**
     * 法人手机号", name = "mobile
     */
    private String mobile;
    /**
     * 证件信息", name = "identityInfo
     */
    private IdentityInfo identityInfo;
    /**
     * 办公地址
     */
    private String address;
    /**
     * 联系邮箱
     */
    private String email;
    /** 性别 */
    private String gender;
    /**
     * 对于在壹账通侧开户失败的即 open_account_status 为 failed 状态的 customer（个人）、business（企业）用户可以在上传完证件后（business 类型还需额外新增联系人）可以再调用此接口重新请求壹账通审核开户。
     * 00新增 01修改
     */
    /** 是否更新user */
    private String updateUser;
    /** 业务方id */
    private String businessId;
    private String accountType;
    private String businessIdType;
    private String thirdUserId;
    private SettleAccountDto settleAccountDto;

    /**
     * 股东信息 最多传5个
     */
    private List<ShareHolderInfoVo> shareHolderInfos;

    /**
     * 受益人信息 最多传5个
     */
    private List<BeneficiaryInfoVo> beneficiaryInfos;

    /**
     * 企业类型
     */
    private String merchantCategory;

    /**
     * 企业联系电话
     */
    private String enterprisePhone;

    private String extraMap;

    /**
     * 授权人/管理员/操作人信息
     */
    private OperatorInfo operatorInfo;
    /**
     * 老的业务商户编号，荷叶洗数据用
     */
    private String oldBusinessUserId;
    /**
     * 三方渠道类型
     */
    private String thirdChannelType;

    /**
     * 获取股东
     */
    public ShareHolderInfoVo getShareHolderInfo() {
        return CollectionUtils.isEmpty(getShareHolderInfos()) ? null : getShareHolderInfos().get(0);
    }

    /**
     * 获取收益人
     */
    public BeneficiaryInfoVo getBeneficiaryInfo() {
        return CollectionUtils.isEmpty(getBeneficiaryInfos()) ? null : getBeneficiaryInfos().get(0);
    }

    /**
     * 适用于富民2.0接口
     * @return
     */
    public List<Map<String, String>> getShareHolderList() {
        if(CollectionUtils.isEmpty(getShareHolderInfos())) {
            return null;
        }

        List<Map<String, String>> shareHolderList = new ArrayList<>();

        for(ShareHolderInfoVo sh : getShareHolderInfos()) {
            Map<String, String> shMap = new HashMap<>();
            shMap.put("name", sh.getShareHolderName());

            if(CertTypeEnum.ID_CARD.getCode().equals(sh.getShareHolderCardType())) {
                shMap.put("cardType", "IDENTITY_CARD");
            }
            else {
                shMap.put("cardType", "UNIFIED_SOCIAL_CREDIT_CODE");
            }

            shMap.put("cardNo", sh.getShareHolderCardNo());
            shMap.put("cardStartDate", sh.getShareHolderCardStartDate());
            shMap.put("cardEndDate", sh.getShareHolderCardEndDate());
            shareHolderList.add(shMap);
        }

        return shareHolderList;
    }

    /**
     * 适用于富民2.0接口
     * @return
     */
    public List<Map<String, String>> getBeneficiaryList() {
        if(CollectionUtils.isEmpty(getBeneficiaryInfos())) {
            return null;
        }

        List<Map<String, String>> beneficiaryList = new ArrayList<>();

        for(BeneficiaryInfoVo be : getBeneficiaryInfos()) {
            Map<String, String> beMap = new HashMap<>();
            beMap.put("name", be.getBeneficiaryName());
            beMap.put("cardType", "IDENTITY_CARD");
            beMap.put("cardNo", be.getBeneficiaryCardNo());
            beMap.put("cardStartDate", be.getBeneficiaryCardStartDate());
            beMap.put("cardEndDate", be.getBeneficiaryCardEndDate());
            beMap.put("phone", be.getBeneficiaryPhone());
            beMap.put("location", be.getBeneficiaryLocation());
            beneficiaryList.add(beMap);
        }

        return beneficiaryList;
    }

    /**
     * 易宝企业商户校验
     */
    public void checkYopMerchantParam() throws PaymentException {
        // ----企业信息----
        if(StringUtils.isBlank(getMerchantCategory())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "企业类型不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getId())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "营业执照号不能为空");
        }

        if(StringUtils.isBlank(getName())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "营业执照名称不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.BUSINESS_CERT_PIC.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "营业执照照片不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getOpenAccountLicenceNo())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户许可证编号（基本存管账户编号）不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.OPEN_ACCOUNT_PIC.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户许可证照片不能为空");
        }

        // ----法人信息----
        if(StringUtils.isBlank(getContactInfos().get(NumberUtils.INTEGER_ZERO).getContactName())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "法人姓名不能为空");
        }

        if(StringUtils.isBlank(getContactInfos().get(NumberUtils.INTEGER_ZERO).getContactCertNo())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "法人身份证号不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.LEGAL_CERT_FRONT.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "法人身份证照片（正面）不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.LEGAL_CERT_BACK.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "法人身份证照片（反面）不能为空");
        }

        // ----联系人信息----
        if(StringUtils.isBlank(getOperatorInfo().getName())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "联系人姓名不能为空");
        }

        if(StringUtils.isBlank(getOperatorInfo().getPhone())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "联系人手机号不能为空");
        }

        if(StringUtils.isBlank(getOperatorInfo().getEmail())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "联系人邮箱不能为空");
        }

        // ----经营地址----
        if(StringUtils.isBlank(getIdentityInfo().getBusinessProvince())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（省）不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getBusinessCity())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（市）不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getBusinessDistrict())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（区/县）不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getBusinessAddress())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（详细地址）不能为空");
        }

        // ----结算账户----
        if(StringUtils.isBlank(getSettleAccountDto().getRecipient().getCardType())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "银行卡类型不能为空");
        }

        if(StringUtils.isBlank(getSettleAccountDto().getRecipient().getAccount())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "银行账号不能为空");
        }

        if(StringUtils.isBlank(getSettleAccountDto().getRecipient().getOpenBankCode())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "银行编码不能为空");
        }
    }

    /**
     * 易宝个人商户校验
     */
    public void checkYopPersonalParam() throws PaymentException {
        // ----个人信息----
        if(StringUtils.isBlank(getName())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户人姓名不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getId())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户人身份证号不能为空");
        }

        if(StringUtils.isBlank(getMobile())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户人手机号不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.PERSONAL_CERT_FRONT.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户人身份证照片（正面）不能为空");
        }

        if(!getPicMap().containsKey(String.valueOf(PictureEnum.PERSONAL_CERT_BACK.getCode()))) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "开户人身份证照片（反面）不能为空");
        }

        // ----经营地址----
        if(StringUtils.isBlank(getIdentityInfo().getBusinessProvince())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（省）不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getBusinessCity())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（市）不能为空");
        }

        if(StringUtils.isBlank(getIdentityInfo().getBusinessDistrict())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "经营地址（区/县）不能为空");
        }

        // ----账户信息----
        if(StringUtils.isBlank(getSettleAccountDto().getRecipient().getAccount())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "银行账号不能为空");
        }

        if(StringUtils.isBlank(getSettleAccountDto().getRecipient().getOpenBankCode())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "银行编码不能为空");
        }
    }
}
